<template>
  <div>
    <title-panel />
    <van-form
      input-align="right"
      error-message-align="right"
      :show-error-message="false"
      show-error
      scroll-to-error
      validate-first
      @failed="onFailed"
      @submit="onSubmit"
    >
      <van-cell class="btnfrom">
        <van-field
          :border="false"
          class="fromInput"
          readonly
          name="motherEarLabelNo"
          label="母羊耳标号"
          :value="obj.motherEarLabelNo"
          required
          is-link
          label-width="50%"
          placeholder="请选择"
          input-align="right"
          :rules="[{ required: true, message: '请选择母羊耳标号' }]"
          @click="chooseMotherEarLabel"
        />
        <van-button native-type="button" plain color="#37B5B5" @click="scanClickA()" class="addbtn" type="primary"
          >扫一扫</van-button
        >
      </van-cell>

      <van-field
        readonly
        v-model="obj.motherPreventionNo"
        name="obj"
        label="母羊畜禽标识编号"
        required
        input-align="right"
      />

      <van-field
        readonly
        :value="obj.breedDate"
        name="breedDate"
        label="配种日期"
        placeholder="请选择时间"
        input-align="right"
        required
        @click="showPicker1 = true"
        :rules="[{ required: true, message: '请选择配种日期' }]"
      />
      <van-popup v-if="!isDisabled" v-model="showPicker1" position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          @confirm="onConfirmStartDate"
          @cancel="showPicker1 = false"
        />
      </van-popup>
      <van-field
        readonly
        :value="obj.estrusDate"
        name="estrusDate"
        label="发情日期"
        placeholder="请选择时间"
        input-align="right"
        required
        @click="showPicker2 = true"
        :rules="[{ required: true, message: '请选择发情日期' }]"
      />
      <van-popup v-if="!isDisabled" v-model="showPicker2" position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          @confirm="onConfirmStartDate2"
          @cancel="showPicker2 = false"
        />
      </van-popup>
      <van-field
        readonly
        name="siteLocation"
        label="配种方式"
        :value="obj.breedTypeCode"
        is-link
        required
        placeholder="请选择配种方式"
        input-align="right"
        :rules="[{ required: true, message: '请选择配种方式' }]"
        @click="natureShows = true"
      />
      <van-popup v-if="!isDisabled" v-model="natureShows" position="bottom">
        <van-picker
          show-toolbar
          :columns="unitS"
          value-key="landParcelName"
          @confirm="onConfirmType"
          @cancel="natureShows = false"
        />
      </van-popup>
      <van-cell class="btnfrom" :border="false">
        <van-field
          class="fromInput"
          :border="false"
          readonly
          name="fatherEarLabelNo"
          label="配种公羊耳标号"
          :value="obj.fatherEarLabelNo"
          required
          is-link
          label-width="50%"
          placeholder="请选择"
          input-align="right"
          :rules="[{ required: true, message: '请选择配种公羊' }]"
          @click="chooseFatherEarLabel"
        />
        <van-button native-type="button" plain color="#37B5B5" @click="scanClick()" class="addbtn" type="primary"
          >扫一扫</van-button
        >
      </van-cell>
      <van-field
        readonly
        v-model="obj.fatherPreventionNo"
        name="obj"
        label="配种公羊畜禽标识编号"
        required
        input-align="right"
      />

      <div class="spacing" />
      <div class="add-btn">
        <van-button
          block
          type="info"
          color="#37B5B5"
          style="width: 85%; margin: auto; margin-top: 50px"
          native-type="submit"
          size="large"
          :loading="loading"
          loading-text="提交中..."
          >保存</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import {
  Form,
  Field,
  Calendar,
  RadioGroup,
  Radio,
  Dialog,
  Toast,
  DatetimePicker,
  Picker,
  Button,
  Icon,
  Popup,
  Cell
} from 'vant'
import UploadImg from '@/components/uploadImg'
import TitlePanel from '@/components/titlePanel'
import dayjs from 'dayjs'
import { getValue, setValue, clearValue } from '@/utils/auth'
import { getHandle, postHandle } from '../../api'
export default {
  components: {
    UploadImg,
    TitlePanel,
    [Form.name]: Form,
    [Field.name]: Field,
    [Calendar.name]: Calendar,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [Button.name]: Button,
    [Icon.name]: Icon,
    [Popup.name]: Popup,
    [Toast.name]: Toast,
    [Cell.name]: Cell,
    [Dialog.Component.name]: Dialog.Component
  },
  data() {
    return {
      unitS: [],
      unitS_Code: [],
      showPicker2: false,
      contentList: '',
      fileInfoList: [],
      currentDate: new Date(),
      showPicker1: false,
      loading: false,
      flag: true,
      //   typeList: [],
      //   resultList: [],
      isDisabled: false,
      natureShows: false,
      natureNameList: [],
      siteList: [],
      natureShow: false,
      natureNameLists: [],
      fertiliList: [],
      obj: {
        motherEarLabelNo: '',
        motherIndividualId: '',
        fatherIndividualId: '',
        breedDate: '',
        estrusDate: '',
        breedTypeCode: '',
        fatherEarLabelNo: ''
      },
      contentObj: {},
      type: null,
      sheepGender: true
    }
  },
  created() {
    this.$bridge.register('backScanData', data => {
      if (this.sheepGender) {
        if (JSON.parse(data).individualId) {
          const obj = {
            earLabelNo: JSON.parse(data).earLabelNo,
            id: JSON.parse(data).individualId,
            motherPreventionNo: JSON.parse(data).preventionNo
          }
          setValue('earTag2d', JSON.stringify(obj))
          this.obj.motherEarLabelNo = JSON.parse(data).earLabelNo
          this.obj.motherIndividualId = JSON.parse(data).individualId

          this.obj.motherPreventionNo = JSON.parse(data).preventionNo
        } else {
          Toast('羊只信息未录入')
        }
      } else {
        if (JSON.parse(data).individualId) {
          const obj1 = {
            earLabelNo: JSON.parse(data).earLabelNo,
            id: JSON.parse(data).individualId,
            fatherPreventionNo: JSON.parse(data).preventionNo
          }
          setValue('earTag2dm', JSON.stringify(obj1))
          this.obj.fatherEarLabelNo = JSON.parse(data).earLabelNo
          this.obj.fatherIndividualId = JSON.parse(data).individualId
          this.obj.fatherPreventionNo = JSON.parse(data).preventionNo
        } else {
          Toast('羊只信息未录入')
        }
      }
    })
    this.$bridge.register('refresh', data => {
      this.getUnitList()

      if (getValue('pzObj')) {
        this.obj = JSON.parse(getValue('pzObj'))
        if (getValue('earTag2d')) {
          this.obj.motherEarLabelNo = JSON.parse(getValue('earTag2d')).earLabelNo
          this.obj.motherIndividualId = JSON.parse(getValue('earTag2d')).id
          this.obj.motherPreventionNo = JSON.parse(getValue('earTag2d')).preventionNo
        }
        if (getValue('earTag2dm')) {
          this.obj.fatherEarLabelNo = JSON.parse(getValue('earTag2dm')).earLabelNo
          this.obj.fatherIndividualId = JSON.parse(getValue('earTag2dm')).id
          this.obj.fatherPreventionNo = JSON.parse(getValue('earTag2dm')).preventionNo
        }
      } else {
        this.getEditData()
      }
    })
  },
  mounted() {},
  methods: {
    scanClick() {
      this.sheepGender = false
      this.$bridge.call('base.scanOneScan')
    },

    scanClickA() {
      this.sheepGender = true
      this.$bridge.call('base.scanOneScan')
    },

    // 跳转选择母羊耳标号,二级单选
    chooseMotherEarLabel() {
      console.log('选择母羊耳标号')
      const earObj = {
        earLabelNo: this.obj.motherEarLabelNo,
        id: this.obj.motherIndividualId
      }
      setValue('pzObj', JSON.stringify(this.obj))
      setValue('fzmSex', '母')
      setValue('earTag2d', JSON.stringify(earObj))
      // 跳转2级单选,用公共2级单选
      this.$bridge.call('addCheckEarTag2d')
    },
    // 跳转选择父羊耳标号,三级单选
    chooseFatherEarLabel() {
      // 跳转2级单选,用公共2级单选
      const earObj = {
        earLabelNo: this.obj.fatherEarLabelNo,
        id: this.obj.fatherIndividualId
      }
      setValue('pzObj', JSON.stringify(this.obj))
      setValue('fzmSex', '公')
      setValue('earTag2dm', JSON.stringify(earObj))
      this.$bridge.call('addCheckEarTag2dm')
    },
    // 请求详情数据
    getEditData() {
      getHandle(`/agri/api/yzbzh/breedRecord/queryDetail/${getValue('breed-view-id')}`).then(res => {
        console.log(res)
        this.obj.motherEarLabelNo = res.data.motherEarLabelNo
        this.obj.motherIndividualId = res.data.motherIndividual.id
        this.obj.breedDate = dayjs(res.data.breedDate).format('YYYY-MM-DD')
        this.obj.estrusDate = dayjs(res.data.estrusDate).format('YYYY-MM-DD')
        this.obj.breedTypeCode = res.data.breedTypeName
        this.obj.fatherIndividual = res.data.fatherIndividual
        this.obj.fatherEarLabelNo = res.data.fatherEarLabelNo
        this.obj.motherPreventionNo = res.data.motherPreventionNo
        this.obj.fatherPreventionNo = res.data.fatherPreventionNo

        this.obj.fatherIndividualId = res.data.fatherIndividual.id
      })
    },
    // 请求单位列表
    getUnitList() {
      this.unitS = []
      getHandle('/agri/api/common/businessData/queryList', { pCode: 'T000010_BREED_TYPE' }).then(res => {
        console.log(res)
        res.data.rows.forEach(item => {
          this.unitS.push(item.name)
          this.unitS_Code.push(item.code)
        })
      })
    },
    // 获取默认数据
    initialize(id) {},
    // 时间
    onConfirmStartDate(value) {
      this.obj.breedDate = dayjs(value).format('YYYY-MM-DD')
      this.showPicker1 = false
    },
    onConfirmStartDate2(value) {
      this.obj.estrusDate = dayjs(value).format('YYYY-MM-DD')
      this.showPicker2 = false
    },
    // 疫苗接种数量这里是多选写的时候改下逻辑
    onConfirmTypes(value) {
      const index = this.fertiliList.indexOf(value)
      if (index == -1) {
        this.fertiliList.push(value)
      } else {
        this.fertiliList.splice(index, 1)
      }
      this.obj.FertilizersPesticides = this.fertiliList.map(item => item.label).join(',')
      this.natureShow = false
    },
    // 选择配种方式
    onConfirmType(value) {
      this.obj.breedTypeCode = value
      this.natureShows = false
    },
    onFailed(errorInfo) {
      Toast(errorInfo.errors[0].message)
    },
    onSubmit(value) {
      this.loading = true
      // let imgInfos = this.obj.imgInfos.map((item) => item.url).join(",");
      // 发送保存网络请求
      this.onSave()
    },
    onSave() {
      const params = {
        motherEarLabelNo: this.obj.motherEarLabelNo ? this.obj.motherEarLabelNo : '',
        motherIndividualId: this.obj.motherIndividualId,
        breedDate: this.obj.breedDate,
        estrusDate: this.obj.estrusDate,
        breedTypeCode: this.unitS_Code[this.unitS.indexOf(this.obj.breedTypeCode)],
        fatherEarLabelNo: this.obj.fatherEarLabelNo ? this.obj.fatherEarLabelNo : '',
        fatherIndividualId: this.obj.fatherIndividualId,
        houseInfoId: getValue('fzmHourseId') || '',
        subjectInfoId: getValue('subjectInfoId'),
        tenantCode: getValue('tenantCode'),
        callChannel: getValue('callChannel'),
        loginUserId: getValue('loginUserId')
      }
      console.log(params, 999)
      debugger
      postHandle(`/agri/api/yzbzh/breedRecord/update/${getValue('breed-view-id')}`, params).then(res => {
        console.log(res)
        // 新增成功跳转列表页
        if (res.status == 0) {
          Toast.success('操作成功')
          clearValue('earTag2d')
          clearValue('earTag2dm')
          clearValue('pzObj')
          this.$bridge.call('base.closePage')
        } else {
          Toast.fail(res.msg || '操作失败')
          this.loading = false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.tip {
  padding-top: 15px;
  color: #999999;
  font-size: 12px;
  line-height: 16px;
  margin: auto;
}
.btn {
  width: 80%;
  margin: 50px auto 0 auto;
}
.btn > div {
  height: 50px;
  background: #2871fe;
  border-radius: 4px;
  text-align: center;
  line-height: 50px;
  color: #ffffff;
  font-size: 17px;
}
.btn .cancel {
  margin-top: 20px;
  background: rgba(40, 113, 254, 0.1);
  border: 1px solid #2871fe;
  color: #2871fe;
}
::v-deep .van-field__label {
  width: 6em;
}
.bthStyle {
  width: 56px;
  height: 20px;
  border-radius: 10px;
  border: 1px solid #2871fe;
}
.active {
  background-color: #fff;
  color: #2871fe;
}
.add-btn {
  padding-bottom: 20px;
}
.addbtn {
  width: 50px;
  height: 24px;
  font-size: 13px;
  padding: 0;
  margin-right: 13px;
  z-index: 10;
  float: right;
  margin-top: -38px;
}
.btnfrom {
  padding: 1px 0;
}
.fromInput {
  width: 86%;
}
</style>
